//
//  CashFormViewController.m
//  MoneyBook
//
//  Created by MyoungHo Kim on 11. 7. 8..
//  Copyright 2011 Daum. All rights reserved.
//

#import "CashFormViewController.h"

@implementation CashFormViewController
@synthesize cashTitle = _cashTitle;
@synthesize defaultSwitch = _defaultSwitch;

@synthesize cashId = _cashId;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                                                  target:self
                                                                                  action:@selector(saveAction)];
        [self.navigationItem setRightBarButtonItem:rightBtn];
        [rightBtn release];
    }
    return self;
}

- (void)dealloc{
    [_cashTitle release];
    [_cashId release];
    [_defaultSwitch release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)viewDidLoad{
    [super viewDidLoad];
    _cashTitle.borderStyle = UITextBorderStyleRoundedRect;
    _cashTitle.textColor = [UIColor blackColor];
    _cashTitle.font = [UIFont systemFontOfSize:15.0];
    _cashTitle.backgroundColor = [UIColor whiteColor];
    _cashTitle.autocorrectionType = UITextAutocorrectionTypeNo;
    _cashTitle.keyboardType = UIKeyboardTypeDefault;
    _cashTitle.returnKeyType = UIReturnKeyDone;
    _cashTitle.clearButtonMode = UITextFieldViewModeWhileEditing;
    _cashTitle.placeholder = @"이름을 입력 해 주세요.";
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload{
    [self setCashTitle:nil];
    [self setCashId:nil];
    [self setDefaultSwitch:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark UIBarButtoncashDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)saveAction{
    SQLHandler *sql = [SQLHandler sharedInstance];
    if(_isCreateMode){
        self.cashId = [NSNumber numberWithInt:[sql createCashWithTitle:[_cashTitle text]
                                                             isDefault:[_defaultSwitch isOn]]
                       ];
    }else{
        //        NSLog(@"saveActoin:  %@",  [_cashTitle text]);
        [sql updateCashId:_cashId 
                    title:[_cashTitle text] 
                isDefault:[_defaultSwitch isOn]];
    }
    if([_defaultSwitch isOn]){
        NSArray *array = [sql findAllCash];
        for(NSDictionary *dic in array){
            if([[dic objectForKey:@"cash_id"] intValue]
               != [self.cashId intValue]){
                NSLog(@"update default no :%d => %d", [[dic objectForKey:@"cash_id"] intValue], [self.cashId intValue]);
                [sql updateCashId:[dic objectForKey:@"cash_id"]
                            title:[dic objectForKey:@"title"]
                        isDefault:NO];
            }
        }
    }

    [[self navigationController] popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark user methods
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)loadCash:(NSNumber*)cashId{
    _isCreateMode = NO;
    NSDictionary *dic = [[SQLHandler sharedInstance] findCashById:cashId];
    //    NSLog(@"dic:%@",dic);
    self.cashId = cashId;
    [self.cashTitle setText:[dic objectForKey:@"title"]];
    if([[dic objectForKey:@"is_default"] isEqualToString:@"y"]){
        [_defaultSwitch setOn:YES];
    }else{
        [_defaultSwitch setOn:NO];
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)createMode{
    _isCreateMode = YES;    
}
@end
